package com.kehutong.applet.admin;

import com.kehutong.common.util.Token;
import com.kehutong.applet.entity.AdvertEntity;
import com.kehutong.applet.service.AdvertService;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

/**
 * @program: kht
 * @description: 广告控制类
 * @author: joneron
 * @create: 2021/5/21
 **/
@MvcAction
@ReqMapping("/admin/crm/applet/advert")
public class AdvertController {

    @Inject
    AdvertService advertService;

    @ReqMapping("/page")
    @HasPermission("crm:applet:advert:view")
    public Object page(JSONObject jsonObject) {
        return advertService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("crm:applet:advert:view")
    public Object list(JSONObject jsonObject) {
        return advertService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("crm:applet:advert:edit")
    @ReqMapping("/save")
    public Object save(AdvertEntity advertEntity, Token token) {
        advertEntity.setCreateName(token.getUsername());
        return advertService.save(advertEntity);
    }

    @Bean(copy=true)
    @HasPermission("crm:applet:advert:edit")
    @ReqMapping("/update")
    public Object update(AdvertEntity advertEntity) {

        return advertService.update(advertEntity);
    }

    @HasPermission("crm:applet:advert:edit")
    @ReqMapping("/delete")
    public Object delete(AdvertEntity advertEntity) {

        return advertService.deleteById(advertEntity.getId());
    }

    @ReqMapping("/setStatus")
    @HasPermission("crm:applet:advert:edit")
    public Object setStatus(AdvertEntity advertEntity) {
        advertEntity.setStatus(!advertEntity.isStatus());
        advertService.update(advertEntity);
        return Result.success();
    }

    @ReqMapping("/get")
    public Object get(AdvertEntity entity) {

        return Result.success(entity);
    }

    @ReqMapping("/clickNum")
    public Object clickNum(AdvertEntity entity) {

        return Result.success(advertService.saveClickNum(entity.getId()));
    }
}
